<?php
require_once(dirname(__FILE__) . '/app.php');

if(!$INI['db']['host']){
	redirect( WEB_ROOT . '/install.php' );
}

$action = $_GET['action'];
if( $action == 'del' && $_GET['id'] > 0 ){
	Preengage::DeleteById($_GET['id'], $login_user_id);
}

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['search_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$day = $getDateArray[2] * 1;

$year = $year < 1 ? $tmp_year : $year;
$month = $month < 1 ? $tmp_mon : $month;
$day = $day < 1 ? $tmp_day : $day;

$startTime = mktime(0, 0, 0, $month, $day, $year);
$endTime = mktime(23, 59, 59, $month, $day, $year);
$bookedList = array();
//if( $defaultShopId < 1 ){
	$bookedList = Preengage::GetAllByUserId($login_user_id, $startTime, $endTime);
$now = time();
/*}else{
	$bookedList = Preengage::GetListByUserIdAndShopId($userId, $defaultShopId);
}*/

$shopList = Shop::GetAllShops();

$serviceList = Service::GetAll();
$serviceIdList = array();
foreach ($serviceList as $k => $v){
	$serviceIdList[$v['id']] = $v['service_name'];
}

$deviceList = Devices::GetAll();
$deviceIdList = array();
foreach ($deviceList as $k => $v){
	$deviceIdList[$v['id']] = $v['device_name'];
}


include template('bookedList');
//include template('subscribe');
